package fragment.livefragments;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.LivePageEntity;
import fragment.AbsBaseFragment;
import fragment.menufragments.MunicipalUnitsFragment;
import utils.adapter.LivePageAdpter;
import volley.VolleySingleton;


/**
 * 直播 fragment
 * Created by dllo on 15/11/17.
 */
public class LivePageFragment extends AbsBaseFragment implements View.OnClickListener {
    private RecyclerView recyclerView;
    private TextView local_fm, national_fm, provice_fm, net_fm,
            recommed_one_tv, recommend_two_tv, recommed_there_tv, fm_more, p_more, pang;
    private NetworkImageView live_One_IV, live_Two_IV, live_There_IV;
    private LivePageAdpter adpter;
    private RecyclerViewHeader viewHeader;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_page_live;
    }

    @Override
    protected void initView() {
        //本地台数据
        local_fm = findView(R.id.local_fm);
        national_fm = findView(R.id.national_fm);
        provice_fm = findView(R.id.provice_fm);
        net_fm = findView(R.id.net_fm);


        //推荐电台数据
        recommed_one_tv = findView(R.id.recommed_one_tv);
        recommend_two_tv = findView(R.id.recommend_two_tv);
        recommed_there_tv = findView(R.id.recommed_there_tv);
        //推荐电台更多按钮

        live_One_IV = findView(R.id.live_One_IV);
        live_Two_IV = findView(R.id.live_Two_IV);
        live_There_IV = findView(R.id.live_There_IV);

//        String tagName="新闻";
//        pang.setText(StringUtil.utf8Encode(tagName));


        //排行更多按钮
        p_more = findView(R.id.p_more);
        p_more.setOnClickListener(this);

        recyclerView = findView(R.id.live_recyclerView);
        GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
        recyclerView.setLayoutManager(manager);
        adpter = new LivePageAdpter();
        recyclerView.setAdapter(adpter);
        viewHeader = findView(R.id.livePageHeader);
        viewHeader.attachTo(recyclerView, true);
        local_fm.setOnClickListener(this);
        national_fm.setOnClickListener(this);
        provice_fm.setOnClickListener(this);
        net_fm.setOnClickListener(this);
    }

    String URLlocal = "http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=1&radioTyp" +
            "e=2&device=android&provinceCode=220000&pageSize=15";
    String UrlNation = "http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=1&radioType" +
            "=1&device=android&pageSize=15";
    String UrlNet = "http://live.ximalaya.com/live-web/v1/getRadiosListByType?" +
            "pageNum=1&radioType=3&device=android&pageSize=15";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.local_fm:
                //向下一个fragment传值
                LiveFmfragment fragment = LiveFmfragment.newInstance(URLlocal, "本地台");
                goToNext(fragment);
                break;
            case R.id.national_fm:
                LiveFmfragment fr = LiveFmfragment.newInstance(UrlNation, "国家台");
                goToNext(fr);
                break;
            case R.id.provice_fm://省市台
                goToNext(new MunicipalUnitsFragment());
                break;
            case R.id.net_fm:
                LiveFmfragment net = LiveFmfragment.newInstance(UrlNet, "网络台");
                goToNext(net);
                break;
            case R.id.p_more:
                LiveMoreFragment moreFragment = new LiveMoreFragment();
                goToNext(moreFragment);
                break;
        }
    }

    @Override
    protected void initData() {

        initNetwork();
    }

    String URL = "http://live.ximalaya.com/live-web/v1/getH" +
            "omePageRadiosList?device=android";

    private void initNetwork() {
        VolleySingleton.getInstance().addRequest(URL,
                LivePageEntity.class, new Response.Listener<LivePageEntity>() {
                    @Override
                    public void onResponse(LivePageEntity response) {
                        if (response != null) {
                            LivePageEntity.ResultEntity result = response.getResult();
                            if (result != null) {
                                List<LivePageEntity.ResultEntity.RecommandRadioListEntity> re
                                        = result.getRecommandRadioList();
                                recommed_one_tv.setText(re.get(0).getRname());
                                recommend_two_tv.setText(re.get(1).getRname());
                                recommed_there_tv.setText(re.get(2).getRname());
                                VolleySingleton.getInstance().
                                        getBitmap(re.get(0).getPicPath(), live_One_IV);
                                VolleySingleton.getInstance().
                                        getBitmap(re.get(1).getPicPath(), live_Two_IV);
                                VolleySingleton.getInstance().
                                        getBitmap(re.get(2).getPicPath(), live_There_IV);

                                List<LivePageEntity.ResultEntity.TopRadioListEntity> top
                                        = result.getTopRadioList();
                                if (top != null) {
                                    adpter.addData(top);
                                }


                            }
                            // adapter.addList(list);
                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }


}
